#!/usr/local/bin/perl -w
use strict;
use utf8;
use lib '../lib';

use NG::Gen::App qw(
NG::Gen::AuthCgi
);

#use JSON::XS;
use NG::Sys::Cfg;
use Data::Dumper;

use NG::Gen::Users::Constants;
my $t;
#my $json;
my $app = new NG::Sys::App();

$app->run;

sub on_prepare_loop {
	my ($app) = @_;
	$t = $app->template_tt->read("main");
	#$json = JSON::XS->new->utf8(0)->pretty(1);
}

sub on_before_process {
	my ($app,$form,$obj) = @_;
	unless($form->{user}){
		$form->redirect("/");
		return 0;
	}
	return 1;
}
sub cmd_default {
	my ($app,$form,$obj) = @_;
	$form->{data}{title} = $form->{user}->title;
	$form->{data}{login} = $form->{user}->login;
	$form->{data}{password} = $form->{user}->chat_pass;
	#$form->{data}{path_to_chat} = $app->{path_to_chat};
	$form->{data}{path_to_img} = $app->{cfg}{path_to_img};

	$form->{data}{setting} = $form->{user}->setting->chat,
	$form->{data}{config} = {
		template_path => "/t",
		template_ver => time(),
		url => $app->{cfg}{path_to_chat},
	};
	my $game = config($app->{cfg}{game});
	$form->{data}{run} = $game->{init}{template};
	$form->{data}{run_data} = $game->{init}{template_data};
	if($game->{init}{start}){
		$form->{data}{start} = $game->{init}{start};
		if($game->{init}{start_data}){
			$form->{data}{start_data} = $game->{init}{start_data};
		}
	}
	if(my $data = $game->{init}{extra_data}){
		unless($form->{user}->validate){
			$data = { %$data };
			$data->{menu} = [
				@{$data->{menu}},{
						command => "mc.game.validate",
						tooltip => "",
						title => "Сохранить",
						selectable => 1,
					},
			];
			$data->{start} = $#{$data->{menu}};
		}
		$form->{data}{extra_data} = $data;
	}
	$form->process($t);
}
